package com.xc.h05.controller.renMengBinController;

import com.github.pagehelper.PageInfo;
import com.xc.h05.entity.Publicity;
import com.xc.h05.service.renMengBinService.PublicityService;
import com.xc.h05.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/publicity")
public class PublicityController {
    @Autowired
    private PublicityService publicityService;

    //查询所有
    @GetMapping("/selectAll")
    public R selectAll(
            @RequestParam(value = "pageNum",required = false,defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize",required = false,defaultValue = "10") int pageSize,
            Publicity publicity
    ){
        System.out.println("pub:"+publicity);
        PageInfo<Publicity> pageInfo = publicityService.selectAll(pageNum, pageSize, publicity);

        return R.ok(pageInfo);
    }
    //添加
    @PostMapping()
    public R add(@RequestBody Publicity publicity){
        int i = publicityService.add(publicity);
        if(i>0){
            return R.ok("添加成功");
        }
        return R.fail("添加失败");
    }
    //删除
    @DeleteMapping("/{id}")
    public R remove(@PathVariable("id") int id){
        int remove = publicityService.remove(id);
        if(remove>0){
            return R.ok("删除成功");
        }else {
            return R.fail("删除失败");
        }
    }
    //修改
    @PutMapping()
    public R update(@RequestBody Publicity publicity){
        int i = publicityService.update(publicity);
        if(i>0){
            return R.ok("修改成功");
        }
        return R.fail("修改失败");
    }
    //批量删除
    @DeleteMapping("/deleteByIds")
    public R deleteByIds(@RequestBody List<Integer> ids){
        int i = publicityService.removeByScope(ids);
        if(i>0){
            return R.ok("批量删除成功");
        }else {
            return R.fail("批量删除失败");
        }
    }
}
